﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GEF1.GEF;

namespace GEF1
{
    public partial class frmMainForm : Form
    {
        List<IEntity> mEntitys = null;

        public frmMainForm()
        {
            InitializeComponent();
            mEntitys = new List<IEntity>();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            int rollingX = 100;

            Entity entity = new Entity(this);
            mEntitys.Add(entity);

            RefreshEntitys();
        }


        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(() => { Form1_Paint(sender, e); }));
            }
            else
            {
                var rect = e.ClipRectangle;
                foreach (IEntityView entity in mEntitys)
                {
                    if (entity.containsRect(rect))
                    {
                        entity.draw();
                    }
                }
            }
        }

        private void RefreshEntitys()
        {
            foreach(IEntityView entity in mEntitys)
            {
                entity.draw();
            }
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(() => { Form1_MouseDown(sender, e); }));
            }
            else
            {
                var point = e.Location;
                foreach (IEntityView entity in mEntitys)
                {
                    entity.select(entity.containsPoint(point));
                }
            }
        }
            

    }
}
